package com.work.controller;/**
 * @author： li
 * @date： 2021-06-01 10:08
 * @version 1.0
 */

import com.work.dao.emaildao;
import com.work.pojo.email;
import com.work.pojo.user;
import com.work.service.mailservice;
import com.work.service.userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author： Administrator
 * @date： 2021-06-01 10:08
 * @version 1.0
 */
@WebServlet("*.mail")
public class mailServlet extends HttpServlet {
    public mailServlet() {super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("GBK");
        String url=req.getServletPath();
        if(url.equals("/email.mail")){
        String cookies=(String)req.getAttribute("cookies");
            String email=(String)req.getAttribute("email");
        //System.out.println("email的"+cookies);

                // 获取 pageSize 和 pageNumber
                String pageSize = req.getParameter("pageSize");
                String pageNumber = req.getParameter("pageNumber");
                // 首页的分页数据初始化
                if("".equals(pageSize)||"".equals(pageNumber)
                        ||null == pageSize || null == pageNumber){
                    pageNumber = "1";
                    pageSize = "3";
                }
                Integer maxPage = mailservice.maxPage(cookies,Integer.parseInt(pageSize));
                //System.out.println("最大页数："+maxPage);
                // 判断是否为最小页数
                if(Integer.parseInt(pageNumber)<=0){
                    pageNumber = "1";
                }
                // 判断是否为最大页数
                if(Integer.parseInt(pageNumber)>=maxPage){
                    pageNumber = maxPage+"";
                }
//Integer.parseInt 字符型转换成整数
                List<email> List = null;
                    List = mailservice.page(cookies,Integer.parseInt(pageSize),
                            (Integer.parseInt(pageNumber)));
                // 放入 request 域中
                req.setAttribute("List",List);
                req.setAttribute("pageNumber",pageNumber);
                req.setAttribute("maxPage",maxPage);
                req.setAttribute("email",email);
            req.getRequestDispatcher("/email.jsp").forward(req,resp);

        }
        else if(url.equals("/list.mail")){
            String cookies=req.getParameter("cookies");
            //System.out.println("list的cookies："+cookies);
            // 获取 pageSize 和 pageNumber
            String pageSize = req.getParameter("pageSize");
            String pageNumber = req.getParameter("pageNumber");
            //System.out.println("mailservlet的size："+pageSize+","+"mailservlet的number:"+pageNumber);
            // 首页的分页数据初始化
            if("".equals(pageSize)||"".equals(pageNumber)
                    ||null == pageSize || null == pageNumber){
                pageNumber = "1";
                pageSize = "3";
            }
            Integer maxPage = mailservice.maxPage(cookies,Integer.parseInt(pageSize));
            // 判断是否为最小页数
            if(Integer.parseInt(pageNumber)<=0){
                pageNumber = "1";
            }
            // 判断是否为最大页数
            if(Integer.parseInt(pageNumber)>=maxPage){
                pageNumber = maxPage+"";
            }
//Integer.parseInt 字符型转换成整数
            List<email> List = null;
            List = mailservice.page(cookies,Integer.parseInt(pageSize),
                    (Integer.parseInt(pageNumber)));
            // 放入 request 域中
            req.setAttribute("List",List);
            req.setAttribute("pageNumber",pageNumber);
            req.setAttribute("maxPage",maxPage);
           // req.setAttribute("cookies",cookies);
            // 跳转至 email.jsp 页面
            req.getRequestDispatcher("/email.jsp").forward(req, resp);
        }
        else if(url.equals("/insert.mail")){
            String cookies=req.getParameter("cookies");
            String title=req.getParameter("title");
            String content=req.getParameter("content");
            email em=new email();
            em.setTitle(title);
            em.setContent(content);
            em.setCookies(cookies);
            int i=0;
            try {
                i=emaildao.insert(em);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if(i>0){
req.setAttribute("msg","写入成功！");
req.getRequestDispatcher("/writemail.jsp").forward(req,resp);

            }
        }else if(url.equals("/delete.mail")){
            String cookies=req.getParameter("cookies");
          String id=req.getParameter("id");
          email em=new email();
          em.setId(Integer.parseInt(id));
          int i=0;
            try {
               i=emaildao.deletebyid(em);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if(i>0)
                req.setAttribute("cookies",cookies);
            req.getRequestDispatcher("/email.mail").forward(req,resp);
        }else if(url.equals("/mailshow.mail")){
            String id=req.getParameter("id");
            email em=new email();
            em.setCookies(id);
            email email=null;
            try {
                email=emaildao.selectbyid(em);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            String title=email.getTitle();
            String content=email.getContent();
            String time=email.getTime();
            String cookies=email.getCookies();
                req.setAttribute("title",title);
            req.setAttribute("content",content);
            req.setAttribute("time",time);
            req.setAttribute("cookies",cookies);
           //System.out.println("数据："+title+content+time+cookies);
                req.getRequestDispatcher("/emailshow.jsp").forward(req,resp);
            }
        }

    @Override
    public void destroy() {
        super.destroy();
    }
}

